{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Balanced cross-validation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "X = np.array([[1, 2], [3, 4], [5, 6], [7, 8],[1, 2], [3, 4], [5, 6], [7, 8]])\n",
    "y = np.array([1, 1, 1, 1, 2, 2, 2, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round 1 : Training indices : [1 2 3 5 6 7] Testing indices : [0 4]\n",
      "Round 2 : Training indices : [0 2 3 4 6 7] Testing indices : [1 5]\n",
      "Round 3 : Training indices : [0 1 3 4 5 7] Testing indices : [2 6]\n",
      "Round 4 : Training indices : [0 1 2 4 5 6] Testing indices : [3 7]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import StratifiedKFold\n",
    "\n",
    "skf = StratifiedKFold(n_splits = 4)\n",
    "\n",
    "cc = 1\n",
    "for train_index, test_index in skf.split(X,y):\n",
    "    print \"Round\",cc,\":\",\n",
    "    print \"Training indices :\", train_index,\n",
    "    print \"Testing indices :\", test_index\n",
    "    cc += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round 1 : Training indices : [6 7 3 1 2 4] Testing indices : [0 5]\n",
      "Round 2 : Training indices : [2 1 0 4 7 6] Testing indices : [5 3]\n",
      "Round 3 : Training indices : [4 3 2 0 5 7] Testing indices : [1 6]\n",
      "Round 4 : Training indices : [1 5 7 2 6 0] Testing indices : [3 4]\n",
      "Round 5 : Training indices : [1 3 4 7 2 5] Testing indices : [0 6]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import StratifiedShuffleSplit\n",
    "\n",
    "sss = StratifiedShuffleSplit(n_splits = 5,test_size=0.25)\n",
    "cc = 1\n",
    "for train_index, test_index in sss.split(X,y):\n",
    "    print \"Round\",cc,\":\",\n",
    "    print \"Training indices :\", train_index,\n",
    "    print \"Testing indices :\", test_index\n",
    "    cc += 1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
